<template>
  <div>
    <el-card class="box-card">
      <!-- 头部信息栏 -->
      <PageTools :totalNum="total">
        <template #formPart>
          <el-form
            :inline="true"
            :model="formInline"
            class="form-inline"
            ref="formInline"
            :rules="rules">
            <el-form-item label="关键字" prop="keyword">
              <el-input v-model="formInline.keyword" placeholder="根据编号搜索"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="resetForm">清除</el-button>
              <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </template>
      </PageTools>

      <!-- 数据列表 -->
      <div class="table-content">
          <el-table
            :data="listData"
            style="width: 100%">
            <el-table-column
              align="center"
              prop="number"
              label="编号"
              width="200">
            </el-table-column>

            <el-table-column
              align="center"
              label="题型"
              width="120">
              <template #default="{row}">
                <span v-if="row.questionType === 1">单选</span>
                <span v-if="row.questionType === 2">多选</span>
                <span v-if="row.questionType === 3">简答</span>
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              prop="username"
              label="题目编号"
              width="260">
              <template #default="{row}">
                <el-button type="text"
                v-for="(item, index) in row.questionNumber"
                :key="index"
                @click="showDetail(item.id)"
                >
                {{item.number}}
                </el-button>
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              prop="createTime"
              label="录入时间"
              width="100">
            </el-table-column>

            <el-table-column
              align="center"
              prop="anserTime"
              label="答题时间"
              width="120">
            </el-table-column>

            <el-table-column
              align="center"
              prop="correctPercent"
              label="正确率"
              width="120">
            </el-table-column>

            <el-table-column
              align="center"
              prop="creator"
              label="录入人"
              width="120">
            </el-table-column>

            <el-table-column align="center" label="操作">
              <template #default = "{row}">
                <el-button type="danger" plain circle icon="el-icon-delete" @click="delGroupList(row.id)"></el-button>
              </template>
            </el-table-column>
          </el-table>
      </div>

      <!-- 分页标签 -->
      <div class="page-divided">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="params.pagesize"
          layout="sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    <previewDialog :visible.sync="visible" :passId="passId"></previewDialog>
  </div>
</template>

<script>
import PageTools from '@/components/PageTools/index.vue'
import arr from '@/api/grouplist.js'
import previewDialog from '../components/previewDialog.vue'
export default {
  name: 'GroupList',
  components: {
    PageTools,
    previewDialog
  },
  data() {
    return {
      visible: false,
      passId: null,
      formInline: {
        keyword: ''
      },
      rules: {
        keyword:[]
      },
      params: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      currentPage: 1,
      listData: []
    }
  },

  methods: {
    resetForm() {
      this.$refs.formInline.resetFields()
    },
    onSearch() {
      if(this.formInline.keyword === '') {
        this.listData = arr.data
        this.total = arr.data.length
      }else {
        this.listData = this.listData.filter(item => item.number === this.formInline.keyword)
      }
    },
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getList(this.params)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.params.page = val
      this.getList(this.params)
    },
    // 查看题目详情
    showDetail(id) {
      this.visible = true
      this.passId = id
    },
    // 删除组题列表
    delGroupList(id) {
      let index = this.listData.findIndex(item => item.id === id)
      this.listData.splice(index, 1)
    }
  },
  created() {
    this.listData = arr.data
    this.total = arr.data.length
  }
}
</script>

<style lang="scss" scoped>
.page-divided{
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
.table-content{
  margin-top: 20px;
}
</style>
